package com.itheima.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.Employee;
import com.itheima.mapper.EmployeeMapper;
import com.itheima.service.EmployeeService;
import com.itheima.vo.R;
import org.springframework.stereotype.Service;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
    @Override
    public R login(Employee employee) {
        if (!StrUtil.isAllNotBlank(employee.getUsername(), employee.getPassword()))
            return R.error("用户名或密码不能为空");
        Employee one = this.getOne(Wrappers.lambdaQuery(Employee.class)
                .eq(Employee::getUsername, employee.getUsername()));
        if (one == null)
            return R.error("该用户不存在");
        String md5 = SecureUtil.md5(employee.getPassword());
        if (!one.getPassword().equalsIgnoreCase(md5))
            return R.error("用户名或密码错误");
        if (one.getStatus()==0)
            return R.error("该账号已被禁用");

        return R.success(one);
    }
}
